//
//  BKXTabBar.swift
//  微博
//
//  Created by 田凯 on 16/4/17.
//  Copyright © 2016年 田凯. All rights reserved.
//

import UIKit
/// 声明一个协议  加号按钮的代理方法
protocol BKXTabBarDelegate: NSObjectProtocol{

//代理方法
    func composeButtonDelegateClick()

}

class BKXTabBar: UITabBar {
    
    // MARK:  问题1      - 定义一个 代理属性  ????? 为啥
    weak var bkxDelegate: BKXTabBarDelegate?
    

    // MARK: - 重写 init frame 方法a
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件爱
        addSubview(composeButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置每个 uitabbarButton的长度
        let len = ScreenW / 5
        //设置第几个标识
        var index: CGFloat = 0
        //遍历tabbar的子控件
        for value in self.subviews{
        //判段 遍历的子控件 是否 是需要的uitabbarbutton 
//            print(value)
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            value.frame.origin.x = index * len
            value.frame.size.width = len
                index++
            
                if index == 2{
                index++
                }
                
            }
        
        }
       
        
        /// 设置加号 button 的frame
        composeButton.center.x = self.frame.width * 0.5
        composeButton.center.y = self.frame.height * 0.5
    }
    
    ///按钮的点击事件
   @objc private func composeButtonClick(){
    print("加号点击了")
    // 实现代理方法
    bkxDelegate?.composeButtonDelegateClick()
    }
    
    
    
    // MARK: -  懒加载加号按钮
   private lazy var composeButton: UIButton = {
    let button = UIButton()
        ///添加按钮的点击事件
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
        /// 设置北京图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        ///设置ima
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        
    return button
    }()
}
